package edu.sctu.zhoumengxue.day201506.day20150616.urlPractice;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by user12 on 2015/6/17.
 */
public class URLPost {

    public void isPost(String path1,String path2){
        String count ="";
        byte[] bytes = new byte[1024];
        try {
            URL url = new URL(path1);

            try {
                HttpURLConnection connection = (HttpURLConnection) url.openConnection();

                connection.setDoInput(true);//允许输入和输出
                connection.setDoOutput(true);

                connection.connect();

                OutputStream out = connection.getOutputStream();//建立输出
                out.write(path2.getBytes());
                out.close();

                InputStream in = connection.getInputStream();//建立输入

                while ((in.read(bytes))!=-1){
                    count = count+new String(bytes);
                    System.out.println(count);
                }

                System.out.println();
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
    }
}
